package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.entity.YddUserData;
import com.itheima.ydd.mapper.YddUserDataMapper;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddCateService;
import com.itheima.ydd.service.IYddUserDataService;
import com.itheima.ydd.service.IYddUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class YddUserDataServiceImpl extends ServiceImpl<YddUserDataMapper, YddUserData> implements IYddUserDataService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private IYddArticleService iYddArticleService;
    @Autowired
    private IYddCateService iYddCateService;

    @Override
    public ResultData userDataHandle(String type, String article_id, String action) {
        YddUserData yddUserData=new YddUserData();
        Integer types = Integer.valueOf(type);
        Integer article_ids = Integer.valueOf(article_id);
        String userId = UserHolder.getUserId();
        if (action.equals("add")){
            yddUserData.setArticleId(article_ids);
            yddUserData.setType(types);
            yddUserData.setUserId(Integer.valueOf(userId));
            save(yddUserData);
            if (types==1){
                YddArticle byId = iYddArticleService.getById(article_ids);
                String lovenum = byId.getLovenum();
                Integer integer = Integer.valueOf(lovenum);
                Integer i= integer+1;
                byId.setLovenum(i+"");
                iYddArticleService.updateById(byId);
            }
        }
        if (action.equals("del")){
            LambdaQueryWrapper<YddUserData>wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(YddUserData::getArticleId, article_ids);
            remove(wrapper);
            if (types==1){
                YddArticle byId = iYddArticleService.getById(article_ids);
                String lovenum = byId.getLovenum();
                final Integer integer = Integer.valueOf(lovenum);
                Integer i=integer-1;
                byId.setLovenum(i+"");
                iYddArticleService.updateById(byId);
            }
        }
        return ResultData.ok("操作成功", null);
    }


    //点赞,收藏
    @Override
    public ResultData userDataLists(Integer page,Integer limit,Integer type) {

        String userId1 = UserHolder.getUserId();
        LambdaQueryWrapper<YddUserData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUserData::getType,type);
        wrapper.eq(YddUserData::getUserId,userId1);
        List<YddUserData> list = list(wrapper);
        if (list==null){
            return ResultData.error("没有数据");
        }
        ResultData getByList = iYddArticleService.getByList(list,page,limit);
        return getByList;
    }

    @Override
    public Integer getnum(Integer id, int i) {
        LambdaQueryWrapper<YddUserData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUserData::getUserId,id);
        wrapper.eq(YddUserData::getType,i);
        List<YddUserData> list = list(wrapper);
        return list.size();
    }


}
